<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem>{{cateGoryName}}</XtxBreadItem>
        </XtxBread>
      <!-- 轮播图 -->
      <XtxSlider :sliders="sliderList" style="height: 500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in cateGoryList" :key="i.id">
            <a href="javascript:;">
              <img :src="i.picture" />
              <p>{{ i.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 各分类推荐模块 -->
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in cateGoryList" :key="item">
        <div class="head">
          <h3>- {{item.name}} -</h3>
          <XtxMore to="/" class="More"/>
        </div>
        <div class="body">
           <GoodItem v-for="good in item.goods" :key="good.id" :good='good'/>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { findBanner } from '@/api/home'
import { findTopCategory } from '@/api/category'
import { onMounted, ref } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import GoodItem from './components/goods-item'
export default {
  name: 'TopCategory',
  components: {
    GoodItem
  },
  setup () {
    // 轮播图区域
    const sliderList = ref([])
    async function getSliderListFn () {
      const res = await findBanner()
      sliderList.value = res.data.result
    }
    onMounted(() => {
      getSliderListFn()
    })

    // 全部分类模块
    const cateGoryList = ref([])
    const cateGoryName = ref('')
    const route = useRoute()
    async function getCateGoryList () {
      const res = await findTopCategory(route.params.id)
      cateGoryList.value = res.data.result.children
      cateGoryName.value = res.data.result.name
    }
    onMounted(() => {
      getCateGoryList()
    })
    onBeforeRouteUpdate(() => {
      getCateGoryList()
    })
    return {
      sliderList,
      cateGoryList,
      cateGoryName
    }
  }
}
</script>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
.body{
  display: flex;
  justify-content: space-between;
}
.head{
  position: relative;
.More{
  position: absolute;
  top: 40px;
  right: 30px;
}
}

</style>
